<?php
session_start ();
require_once ("../commun/ouvre_base.php");
// ~ $link = mysqli_connect($host, $userbase, $passwd) or die('Impossible de se connecter pour récupérer niveau : ' . mysql_error());
mysql_select_db ( $db ) or die ( 'Impossible de s&#233;lectionner la base de donn&#233;es' );
// ~ mysqli_select_db($link,$db) or die('Impossible de s&#233;lectionner la base de donn&#233;es');

$_SESSION ['pathway'] = '..';
$_SESSION ['pathway_js'] = '/genetiquespataxeurospa';
include_once ("../header.php");

if (! isset ( $_SESSION ['id'] )) {
	echo "<table class='principale'><tr><td>Session expired</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/index.php'>Start</a></table>";
} elseif ($_SESSION ['acces_genetique'] == 0) {
	echo "<table class='principale'><tr><td>Restricted area</td></tr>";
	echo "<a href='" . $_SESSION ['pathway'] . "/start.php'>Home</a></table>";
} else {
	
	$formatdate = $_SESSION ['date'];
	$action = $_GET ['action']; // edit ou insert
	if ($action == 'edit') {
		echo "<img src='" . $_SESSION ['pathway'] . "/images/modifier.png' height='20' alt='modifier.png' /> ";
	} else if ($action == 'insert') {
		echo "<img src='" . $_SESSION ['pathway'] . "/images/add-icon.png' height='20' alt='button_add.png' /> ";
	}
	echo ucfirst ( $action ) . " whole genome analysis</p>";
	$table = $_GET ['table']; // edit ou insert
	
	$hash_champ_valeur = array ();
	if ($action == 'edit') { // on récupère les données dans la index déjà dans la base
		$index = $_GET ['index'];
		
		// ~ ------------------------ en cas de modifs sur les fichiers
		$query_fichiersattaches = "SELECT `fichiersattaches`
		FROM `wholegenomeanalysis`
		WHERE `index` LIKE " . $_GET ['index'];
		// ~ echo $query_fichiersattaches;
		$resultat_fichiersattaches = mysql_query ( $query_fichiersattaches ) or die ( "impossible de remplir le questionnaire : " . mysql_error () );
		// ~ $resultat_fichiersattaches = mysqli_query($link,$query_fichiersattaches) or die("impossible de remplir le questionnaire : " . mysql_error());
		$fichiers_attaches_base = mysql_fetch_array ( $resultat_fichiersattaches );
		// ~ $fichiers_attaches_base = mysqli_fetch_array($resultat_fichiersattaches);
		
		$fichiersattaches = $fichiers_attaches_base ['fichiersattaches'];
		if (isset ( $_GET ['delete'] )) {
			$fichiersattaches = str_replace ( $_GET ['delete'] . ';', '', $fichiersattaches );
			unlink ( "../uploads/" . $_GET ['delete'] );
			$query_update_fichiers = "UPDATE `spatax`.`wholegenomeanalysis` SET `fichiersattaches` = '$fichiersattaches' WHERE `wholegenomeanalysis`.`index` = " . $_GET ['index'];
			mysql_query ( $query_update_fichiers ) or die ( "impossible d'updater fichiers : " . mysql_error () );
			// ~ mysqli_query($link,$query_update_fichiers) or die("impossible d'updater fichiers : " . mysql_error());
		}
		
		$requete_index = "SELECT * FROM `" . $table . "`
		WHERE `index` LIKE " . $_GET ['index'] . ";";
		// ~ echo $requete_index;
		$result_requete_index = mysql_query ( $requete_index ) or die ( "Impossible de recupérer les entetes : " . mysql_error () );
		// ~ $result_requete_index = mysqli_query($link,$requete_index) or die("Impossible de recupérer les entetes : " . mysql_error());
		
		while ( $ligne = mysql_fetch_assoc ( $result_requete_index ) ) {
			// ~ while($ligne = mysqli_fetch_assoc($result_requete_index)) {
			foreach ( $ligne as $champ => $valeur ) {
				$hash_champ_valeur [$champ] = $valeur;
			}
		}
		$code1 = $hash_champ_valeur ['code1'];
		$code2 = $hash_champ_valeur ['code2'];
		$code3 = $hash_champ_valeur ['code3'];
		$code4 = $hash_champ_valeur ['code4'];
		$code5 = $hash_champ_valeur ['code5'];
		$status = $hash_champ_valeur ['statut'];
		$validation = $hash_champ_valeur ['validation'];
	} elseif ($action == 'insert') {
		require_once ("../requetes/recupere_max.php");
		
		if (isset ( $_SESSION ['code1'] )) {
			$code1 = $_SESSION ['code1'];
		}
		if (isset ( $_SESSION ['code2'] )) {
			$code2 = $_SESSION ['code2'];
		}
		if (isset ( $_SESSION ['code3'] )) {
			$code3 = $_SESSION ['code3'];
		}
		if (isset ( $_SESSION ['code4'] )) {
			$code4 = $_SESSION ['code4'];
		}
		if (isset ( $_SESSION ['code5'] )) {
			$code5 = $_SESSION ['code5'];
		}
		$status = $_SESSION ['status'];
		$validation = $_SESSION ['validation'];
	} else {
		echo "error";
	}
	if (isset ( $_FILES ['fichieraajouter'] ) and $_FILES ['fichieraajouter'] ['error'] == 0) {
		$tabledate = getdate ();
		$id_unique = $tabledate ['seconds'] . $tabledate ['minutes'] . $tabledate ['hours'] . $tabledate ['mday'] . $tabledate ['mon'] . $tabledate ['year'];
		
		move_uploaded_file ( $_FILES ['fichieraajouter'] ['tmp_name'], '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) );
		chmod ( '../../uploads/' . $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ), 0777 );
		$hash_champ_valeur ['fichiersattaches'] .= $id_unique . '_' . basename ( $_FILES ['fichieraajouter'] ['name'] ) . ";";
	}
	// ~ print_r($hash_champ_valeur);
	
	?>



<!-- wholegenomeanalysis --------------------------->

<?php
	$disabled_tous = '';
	if ($_SESSION ['acces_genetique'] >= 2) {
		echo "<form action=\"./requetes/remplissage_wholegenomeanalysis.php?action=$action&amp;index=$index&amp;table=wholegenomeanalysis\" method='post' enctype='multipart/form-data'>";
	} else {
		$disabled_tous = 'disabled';
	}
	$disabled_droits_supremes = '';
	if ($_SESSION ['acces_genetique'] < 3) {
		$disabled_droits_supremes = 'disabled';
	}
	?>

<table class='principale'>
	<tr>
		<td><i>user : </i><input <?php echo $disabled_tous.' ';?> type="text"
			name="user" readonly="readonly" style="width: 200px; border: none;"
			value="<?php
	if (isset ( $hash_champ_valeur ['user'] ) and $hash_champ_valeur ['user'] != '') { // on considère que le champ est egal à '' uniquement lors de la création de la fiche qu'il n'est jamais vide après
		echo strtoupper ( $hash_champ_valeur ['user'] );
	} else {
		$hash_champ_valeur ['user'] = $_SESSION ['id'];
		echo strtoupper ( $hash_champ_valeur ['user'] );
	}
	?>" /></td>
		<td><i>Last modifier : </i><input <?php echo $disabled_tous;?>
			type="text" name="last_modifier" readonly="readonly"
			style="width: 200px; border: none;"
			value="<?php
	if (isset ( $hash_champ_valeur ['last_modifier'] ) and $hash_champ_valeur ['last_modifier'] != '') {
		echo strtoupper ( $hash_champ_valeur ['last_modifier'] );
	} else {
		echo strtoupper ( $hash_champ_valeur ['user'] );
	}
	?>" /></td>
	</tr>
</table>

<form
	action="./requetes/remplissage_wholegenomeanalysis.php?action=<?php echo $action; ?>&amp;index=<?php echo $index; ?>&amp;table=wholegenomeanalysis"
	method='post' enctype='multipart/form-data'>

	<table class='principale'>
		<tr>
			<td>Code ID Family : <br /> <input <?php echo $disabled_tous.' ';?>
				type="text" maxlength="3" size="3" name="code1" id="code1"
				value='<?php if(isset($code1)){echo $code1;} ?>' />- <input
				<?php echo $disabled_tous.' ';?> type="text" maxlength="3" size="3"
				name="code2" id="code2"
				value='<?php if(isset($code2)){echo $code2;} ?>' />- <input
				<?php echo $disabled_tous.' ';?> type="text" maxlength="3" size="3"
				name="code3" id="code3"
				value='<?php if(isset($code3)){echo $code3;} ?>' />- <input
				<?php echo $disabled_tous.' ';?> type="text" maxlength="4" size="4"
				name="code4" id="code4"
				value='<?php if(isset($code4)){echo $code4;} ?>' />
			</td>
			<td>Individual&#40;s&#41; : <br /> <input
				<?php echo $disabled_tous.' ';?> type='text' style='width: 100px;'
				name='code5' id="code5"
				value='<?php if(isset($code5)){echo $code5;} ?>' />&#42;&#40;&#34;&#59;&#34;
				as separator&#41;
			</td>
			<td align='center'>and/or :</td>
			<td>External code : <br /> <input <?php echo $disabled_tous.' ';?>
				type='text' style='width: 100px;' name='codeexterne'
				value='<?php if(isset($hash_champ_valeur['codeexterne'])){echo $hash_champ_valeur['codeexterne'];} ?>' />&#42;
			</td>
			<td>Genethon code : <br /> <input <?php echo $disabled_tous.' ';?>
				type='text' style='width: 100px;' name='codegenethon'
				value='<?php if(isset($hash_champ_valeur['codegenethon'])){echo $hash_champ_valeur['codegenethon'];} ?>' /></td>
		</tr>
		<tr>
			<td>Center : <br /> <input <?php echo $disabled_tous.' ';?>
				type='text' style='width: 100px;' name='centre'
				value='<?php if(isset($hash_champ_valeur['centre'])){echo $hash_champ_valeur['centre'];} ?>' /></td>
			<td>Neurologist/Biologist : <br /> <input
				<?php echo $disabled_tous.' ';?> type='text' style='width: 100px;'
				name='neurologist'
				value='<?php if(isset($hash_champ_valeur['neurologist'])){echo $hash_champ_valeur['neurologist'];} ?>' /></td>
		</tr>
	</table>


	<table class='principale'>
		<tr>
			<td>Date :</td>
			<td><input <?php echo $disabled_tous.' ';?> type='text'
				style='width: 100px;' name='date'
				value='<?php if(isset($hash_champ_valeur['date']) and $hash_champ_valeur['date'] != ''){ echo $hash_champ_valeur['date']; }else{ echo $_SESSION['date'];} ?>' />&#42;(yyyy-mm-dd)</td>
			<td></td>
			<td style="border: 1px solid #BCD0D6; padding: 10px;">Status : <select
				size='1' name='statut'>
					<option></option>
					<option value='ongoing'
						<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'ongoing'){ echo "selected='selected'"; } ?>>
						ongoing</option>
					<option value='done'
						<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'done'){ echo "selected='selected'"; } ?>>
						done</option>
					<option value='to do'
						<?php if(isset($hash_champ_valeur['statut']) and $hash_champ_valeur['statut'] == 'to do'){ echo "selected='selected'"; } ?>>
						to do</option>
			</select></td>
		</tr>
	<?php
	if ($_SESSION ['acces_genetique'] >= 3) {
		echo "<tr><td>user : </td><td><input $disabled_tous type='text' style='width:100px' name='user' value='";
		if (isset ( $hash_champ_valeur ['user'] ) and $hash_champ_valeur ['user'] != '') {
			echo $hash_champ_valeur ['user'];
		} else {
			echo strtoupper ( $_SESSION ['id'] );
		}
		echo "' /></td></tr>";
	}
	?>
	<tr>
			<td>Technique :</td>
			<td><select name='techniquewholegenomeanalysis' size='1'>
					<option></option>
					<option value='high density microarrays'
						<?php if(isset($hash_champ_valeur['techniquewholegenomeanalysis']) and $hash_champ_valeur['techniquewholegenomeanalysis'] == 'high density microarrays'){ echo "selected='selected'"; } ?>>high
						density microarrays</option>
					<option value='linkage microarrays'
						<?php if(isset($hash_champ_valeur['techniquewholegenomeanalysis']) and $hash_champ_valeur['techniquewholegenomeanalysis'] == 'linkage microarrays'){ echo "selected='selected'"; } ?>>linkage
						microarrays</option>
					<option value='exome'
						<?php if(isset($hash_champ_valeur['techniquewholegenomeanalysis']) and $hash_champ_valeur['techniquewholegenomeanalysis'] == 'exome'){ echo "selected='selected'"; } ?>>exome</option>
					<option value='high throughput sequencing'
						<?php if(isset($hash_champ_valeur['techniquewholegenomeanalysis']) and $hash_champ_valeur['techniquewholegenomeanalysis'] == 'high throughput sequencing'){ echo "selected='selected'"; } ?>>high
						throughput sequencing</option>
			</select></td>
			<td>Comments : <br /> <textarea <?php echo $disabled_tous.' ';?>
					name='commentairetechnique' cols='20' rows='1'><?php if(isset($hash_champ_valeur['commentairetechnique'])){ echo $hash_champ_valeur['commentairetechnique'];} ?></textarea></td>
			<td>Material : <input <?php echo $disabled_tous.' ';?> type='text'
				style='width: 200px;' name='materialwholegenomeanalysis'
				id='autocompletionmaterial'
				value='<?php if(isset($hash_champ_valeur['materialwholegenomeanalysis'])){ echo $hash_champ_valeur['materialwholegenomeanalysis'];} ?>' /></td>
		</tr>
		<tr>
			<td>Variants :</td>
			<td><textarea <?php echo $disabled_tous.' ';?> name='variants'
					cols='20' rows='10'><?php if(isset($hash_champ_valeur['variants'])){ echo $hash_champ_valeur['variants']; } ?></textarea></td>
			<td>Comments : <br /> <textarea <?php echo $disabled_tous.' ';?>
					name='commentairewholegenomeanalysis' cols='20' rows='1'><?php if(isset($hash_champ_valeur['commentairewholegenomeanalysis'])){ echo $hash_champ_valeur['commentairewholegenomeanalysis'];} ?></textarea></td>
		</tr>
		<tr>
			<td>Attached file(s) :</td>
			<td>
	<?php
	
	if (isset ( $hash_champ_valeur ['fichiersattaches'] )) {
		$table_fichiers = explode ( ';', $hash_champ_valeur ['fichiersattaches'] );
		foreach ( $table_fichiers as $fichier_attache ) {
			if ($fichier_attache != '') {
				echo "<img src='" . $_SESSION ['pathway'] . "/images/poubelle.png' height='15' onclick=\"confirmer_suppression_fichier('" . $code1 . '-' . $code2 . '-' . $code3 . '-' . $code4 . '-' . $code5 . "','$index','$table','$fichier_attache')\" /> <a target=_blank href='../uploads/$fichier_attache'>$fichier_attache</a><br />"; // on affiche juste la liste des fichiers déjà présents dans la base
			}
		}
	}
	
	?></td>

			<input <?php echo $disabled_tous.' ';?> type='hidden'
				id='fichiersattaches' name='fichiersattaches'
				value='<?php if(isset($hash_champ_valeur['fichiersattaches'])){ echo $hash_champ_valeur['fichiersattaches']; } ?>' />
			<td><input <?php echo $disabled_tous.' ';?> type='file'
				name='fichieraajouter' size="10" value='' /><input
				<?php echo $disabled_tous.' ';?> type='submit' name='submitprinc'
				value='upload' /><i> (max 10)</i></td>
		</tr>
	</table>

	<table class='principale'>
		<tr>
			<td><h3>Diagnosis :</h3></td>
			<td><textarea <?php echo $disabled_tous.' ';?>
					name='diagnosticwholegenomeanalysis' cols='15' rows='3'><?php if(isset($hash_champ_valeur['diagnosticwholegenomeanalysis'])){ echo $hash_champ_valeur['diagnosticwholegenomeanalysis'];} ?></textarea></td>
		</tr>
		<tr>
			<td><h3>Validation :</h3></td>

			<td>
	<?php
	echo "<label><input " . $disabled_tous . ' ' . $disabled_droits_supremes . ' ' . "type='radio' name='validation' value='yes' ";
	if (isset ( $validation ) and $validation == 'yes') {
		echo "checked='checked'";
	}
	echo "/> yes </label><br />";
	echo "<label><input " . $disabled_tous . ' ' . $disabled_droits_supremes . ' ' . "type='radio' name='validation' value='no' ";
	if (isset ( $validation ) and $validation == 'no' or isset ( $validation ) and $validation == '') {
		echo "checked='checked'";
	}
	echo "/> no </label>";
	?></td>
		</tr>

		<tr>
			<td><h3>To do :</h3></td>
			<td><textarea <?php echo $disabled_tous.' ';?> name='afaire'
					cols='15' rows='3'><?php if(isset($hash_champ_valeur['afaire'])){ echo $hash_champ_valeur['afaire'];} ?></textarea></td>
		</tr>
		<tr>
			<td><h3>Comments :</h3></td>
			<td><textarea <?php echo $disabled_tous.' ';?>
					name='commentairesconclusion' cols='15' rows='3'><?php if(isset($hash_champ_valeur['commentairesconclusion'])){ echo $hash_champ_valeur['commentairesconclusion'];} ?></textarea></td>
		</tr>
		<tr>
			<td><input <?php echo $disabled_tous.' ';?> type='submit'
				name='submitprinc' value='Ok' /></td>
		</tr>
	</table>

</form>

<?php
}

include ("../footer.php");
?>
